﻿#include <qpainter.h>
#include "direntry.h"

DirEntry::DirEntry(QWidget *parent): QMainWindow(parent) 
{
	render = new QSvgRenderer();
	ui.setupUi(this);
}
DirEntry::~DirEntry() {

}

#pragma region 目录显示
void DirEntry::showDirDetail(QPainter *painter, QPoint pos, int point_to, int select_to)
{
	int i;
	for (i = 0; i < dir_show.file_num; i++)
	{

		if (point_to == i)
		{
			QRectF rect3(-10, pos.y() + 30 * i - 3, 980, 30);
			painter->setBrush(QColor(0, 0, 0, 0));
			QPen pen(QColor(63, 63, 70, 255));
			pen.setStyle(Qt::SolidLine);
			pen.setWidth(2);
			painter->setPen(pen);
			painter->drawRect(rect3);
		}
		if (select_to == i)
		{
			QRectF rect4(-10, pos.y() + 30 * i - 3, 980, 30);
			painter->setBrush(QColor(63, 63, 70));
			painter->setPen(QColor(0, 0, 0, 0));
			painter->drawRect(rect4);
		}

		if (dir_show.entry[i].type == Directory)
		{
			painter->setPen(QColor(50, 147, 210));
		}
		else if (dir_show.entry[i].type == otherFile)
		{
			painter->setPen(QColor(218, 218, 218));
		}
		else if (dir_show.entry[i].type == txt)
		{
			painter->setPen(QColor(214, 157, 113));
		}
		else if (dir_show.entry[i].type == bin)
		{
			painter->setPen(QColor(78, 210, 176));
		}

		//绘制图标
		QRectF rect(pos.x(), pos.y() + 30 * i, 30, 25);
		render->load(icon_path[dir_show.entry[i].type]);
		render->render(painter, rect);

		//绘制文件名
		QFont font("宋体", 10);
		font.setBold(true);
		painter->setFont(font);
		QRectF rect2(pos.x() + 36, pos.y() + 30 * i + 3, 720, 27);
		painter->drawText(rect2, Qt::AlignLeft, QString(dir_show.entry[i].name));
	}
}
#pragma endregion

#pragma region 目录显示之前的预备内容
int DirEntry::getDirNow()
{
	dir_show = show_dir(inode_num);
	if (dir_show.file_num >= 17)
	{
		mScrollDirDownLimit = 1;
	}
	dirposition_h = dir_show.file_num * 30;
	return dirposition_h;
}
void DirEntry::fadeOutEntry(QPainter *painter, QPoint pos)
{
	//没打算做了
}
void DirEntry::enter_next_dir(int i)
{
	enter_dir(inode_num, dir_show.entry[i].name);
	change_path(dir_show.entry[i].name);//改变路径前缀
	return;
}
FileType DirEntry::getFileType(int i)
{
	return dir_show.entry[i].type;
}
void DirEntry::write_file_to_cp(int i)
{
	file_read(dir_show.entry[i].name);
}
int DirEntry::write_file_to_disk(FileType t)
{
	QFileInfo fileinfo(BUFF);
	QByteArray tmp = fileinfo.fileName().toLocal8Bit();
	char fname[1024];
	memcpy(fname, tmp.data(), tmp.size() + 1);
	int i = make_file(inode_num, fname, t);
	file_write(fname);
	return i;
}
void DirEntry::deleteFile(int i)
{
	del_file(inode_num, dir_show.entry[i].name, 0);
}
#pragma endregion

